# -*- coding: utf-8 -*-
import logging
from selenium.common.exceptions import TimeoutException, NoSuchElementException

from pages.base_page import BasePage
from objectsmap.page_locators import Signup
from pages_exceptions.pages_exceptions import SignupException


class SignupPage(BasePage):
    
    TITLE = 'New User Registration ~ IASO'
    LOCATION = 'https://gfisoftware.force.com/CommunitiesSelfReg'

    def __init__(self, driver):
        super(SignupPage, self).__init__(driver, SignupPage.LOCATION)

    def is_right_location(self):
        super(SignupPage, self).is_right_location(SignupPage.TITLE)

    def signup(self, user_signup):
        try:
            first_name_element = self._driver.find_element(*Signup.IN_FIRSTNAME)
            first_name_element.send_keys(user_signup['first_name'])

            last_name_element = self._driver.find_element(*Signup.IN_LASTNAME)
            last_name_element.send_keys(user_signup['last_name'])

            nickname_element = self._driver.find_element(*Signup.IN_NICKNAME)
            nickname_element.send_keys(user_signup['nickname'])

            email_element = self._driver.find_element(*Signup.IN_EMAIL)
            email_element.send_keys(user_signup['email'])

            password_element = self._driver.find_element(*Signup.IN_PASSWORD)
            password_element.send_keys(user_signup['password'])

            confirm_password_element = self._driver.find_element(*Signup.IN_CONFIRM_PASSWORD)
            confirm_password_element.send_keys(user_signup['confirm_password'])

            self._driver.find_element(*Signup.BTN_SUBMIT).submit()
            logging.info('signup success')
        except (TimeoutException, NoSuchElementException) as e:
            raise SignupException('Can\'t signup')
